package com.think.teaching.controller;

import com.think.common.controller.BaseController;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.ActionType;
import com.think.teaching.service.impl.ActionTypeServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 事件类型 前端控制器
 * @author 赵兴
 * @since 2020-11-12
 */

@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "事件类型接口")
public class ActionTypeController extends BaseController{
	
	private final ActionTypeServiceImpl actionTypeService;
	
	public ActionTypeController(ActionTypeServiceImpl actionTypeService)
	{
		this.actionTypeService = actionTypeService;
	}
	
	@ApiOperation("添加新的事件类型")
	@PostMapping("admin/actionType")
	public Result AddActionType(@RequestBody() ActionType data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			//事件名称不得重复
			if(actionTypeService.GetActionTypeCountByName(data.getName())>0)
				return Result.failure(ResultCode.ACTIONTYPE_ALREADY_EXIST);
			
			ActionType actionType = actionTypeService.AddActionType(data);
			if (actionType != null) {
				return Result.success(actionType);
			} else {
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新事件类型")
	@RequestMapping(value = "admin/actionType", method = RequestMethod.PUT)
	public Result UpdateActionType(@RequestBody() ActionType data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			ActionType actionType =actionTypeService.GetActionTypeByType(data.getType());
			if(actionType == null || actionType.getId().equals(data.getId())) {
				actionType = actionTypeService.UpdateActionType(data);
				if (actionType != null) {
					return Result.success(actionType);
				} else {
					return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
				}
			}
			else {
				return Result.failure(ResultCode.ACTIONTYPE_ALREADY_EXIST);
			}
			
		} else
			return result;
	}

	@ApiOperation("删除事件类型")
	@RequestMapping(value = "admin/actionType/{id}", method = RequestMethod.DELETE)
	public Result DeleteActionType(@PathVariable("id") Integer id) {
		ActionType actionType = actionTypeService.GetActionType(id,false);
		if (actionType == null) {
			return Result.failure(ResultCode.ACTIONTYPE_NOT_EXIST);
		} else {
			if (actionTypeService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}

	@ApiOperation("管理员获取事件类型详情")
	@RequestMapping(value = "admin/actionType/{id}", method = RequestMethod.GET)
	public Result AdminGetActionType(@PathVariable("id") Integer id) {
		ActionType actionType = actionTypeService.GetActionType(id,false);
		if( actionType== null) {
			return Result.failure(ResultCode.ACTIONTYPE_NOT_EXIST);
		}
		else {
			
			return Result.success(actionType);
		}
	}
	
	@ApiOperation("用户获取事件类型详情")
	@RequestMapping(value = "user/actionType/{id}", method = RequestMethod.GET)
	public Result GetActionType(@PathVariable("id") Integer id) {
		ActionType actionType = actionTypeService.GetActionType(id,true);
		if( actionType== null) {
			return Result.failure(ResultCode.ACTIONTYPE_NOT_EXIST);
		}
		else {
			
			return Result.success(actionType);
		}
	}

	@ApiOperation("管理员修改事件类型状态")
	@RequestMapping(value = "admin/actionType/{id}/{state}", method = RequestMethod.PUT)
	public Result UpdateActionTypeState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
		ActionType actionType = actionTypeService.GetActionType(id,false);
		if( actionType== null) {
			return Result.failure(ResultCode.ACTIONTYPE_NOT_EXIST);
		}
		else {
			try {
				actionType = actionTypeService.SetActionTypeState(id, state);
				return Result.success(actionType);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("用户搜索事件类型")
	@RequestMapping(value = "user/actionType/search", method = RequestMethod.GET)
	public Result UserSearchActionType(@RequestParam("key") String key,@RequestParam("type")Integer type) {
		return Result.success(actionTypeService.ListActionTypeSearch(key,type, true));
	}

	@ApiOperation("管理员搜索事件类型")
	@RequestMapping(value = "admin/actionType/search", method = RequestMethod.GET)
	public Result AdminSearchActionType(@RequestParam("key") String key,@RequestParam("type") Integer type) {
		return Result.success(actionTypeService.ListActionTypeSearch(key,type, false));
	}

	@ApiOperation("管理员获取事件类型列表")
	@RequestMapping(value = "admin/actionTypes/{page}/{count}", method = RequestMethod.GET)
	public Result AdminListActionType(@PathVariable("page") Integer page, @PathVariable("count") Integer count,@RequestParam("type")Integer type) {
		return Result.success(actionTypeService.ListActionTypeWithType(count, page, type, false));
	}

	@ApiOperation("用户获取事件类型列表")
	@RequestMapping(value = "user/actionTypes/{page}/{count}", method = RequestMethod.GET)
	public Result UserListActionType(@PathVariable("page") Integer page, @PathVariable("count") Integer count,@RequestParam("type")Integer type) {
		return Result.success(actionTypeService.ListActionTypeWithType(count, page, type, true));
	}
}
